

function binarySearch (arr, target, left, right) {
  if (arr.length === 0) {
    return -1;
  }

  if (left > right) {
    return -1;
  }

  const midIndex = Math.floor((left + right) / 2);
  const midValue = arr[midIndex];

  if (target === midValue) return midIndex;

  if (midValue > target) {
    return binarySearch(arr, target, left, midIndex - 1);
  } else {
    return binarySearch(arr, target, midIndex + 1, right);
  }

}




function binarySearch2 (arr, target) {
  if (arr.length === 0) {
    return -1;
  }
  let right = arr.length -1;

  for (let left = 0; left <= right;) {
    // 循环的时候 等于 = 还会走一圈；
    const midIndex = Math.floor((left + right) / 2);
    const midValue = arr[midIndex];
    if (target === midValue) return midIndex;
    if (midValue > target) {
      right = midIndex - 1;
    } else {
      left = midIndex + 1;
    }
  }
  return - 1;
}
console.log(binarySearch([1, 2, 3, 4, 5, 6, 7], 1, 0, 6),'log');
console.log(binarySearch2([1, 2, 3, 4, 5, 6, 7],1),'log');
